package com.ddd.core.exception;

import com.ddd.core.api.IResultCode;
import com.ddd.core.constants.enums.ApiCodeEnum;
import lombok.Getter;

/**
 * 业务异常,当业务逻辑发生异常后，将手动抛出该异常中止流程执行
 *
 * @author zhongwenbin
 * @date 2023/6/24 22:23
 */
@Getter
public class BizException extends RuntimeException {

    /**
     * 错误码
     */
    String code;
    /**
     * 状态信息
     */
    String message;
    /**
     * 内容
     */
    Object data;
    /**
     * 内部信息
     */
    String innerMsg;

    public BizException(String message) {
        super(message);
        this.message = message;
        this.code = ApiCodeEnum.FAILED.getCode();

    }

    public BizException(String message, Object data) {
        super(message);
        this.message = message;
        this.data = data;
    }

    public BizException(String message, Throwable cause) {
        super(message, cause);
        this.message = message;
        this.code = ApiCodeEnum.FAILED.getCode();
    }

    public BizException(String message, String code, Object data, String innerMsg) {
        super(message);
        this.message = message;
        this.code = code;
        this.data = data;
        this.innerMsg = innerMsg;
    }

    public BizException(String message, Throwable cause, String code, Object data, String innerMsg) {
        super(message, cause);
        this.code = code;
        this.message = message;
        this.data = data;
        this.innerMsg = innerMsg;
    }

    public static BizException of(String message) {return new BizException(message);}

    public static BizException of(String message, String code) {
        return new BizException(message, code, null, null);
    }

    public static BizException of(String message, Object data) {
        return new BizException(message, data);
    }

    public static BizException of(IResultCode errorCode, String message) {
        return new BizException(message, errorCode.getCode(), null, errorCode.getInnerMsg());
    }


}
